package leetcode;

public class Q0289 {
    public void gameOfLife(int[][] board) {
        for (int i = 0; i < board.length; i++) {
            for (int j = 0; j < board[0].length; j++) {
                int live = 0;
                for (int k = i - 1; k <= i + 1; k++) {
                    for (int l = j - 1; l <= j + 1; l++) {
                        if (k >= 0 && k < board.length && l >= 0 && l < board[0].length && (k != i || l != j) && (board[k][l] == 1 || board[k][l] == 2))
                            live++;
                    }
                }
                if ((live < 2 || live > 3) && board[i][j] == 1) {
                    board[i][j] = 2;
                } else if (live == 3 && board[i][j] == 0) {
                    board[i][j] = 3;
                }
            }
        }

        for (int i = 0; i < board.length; i++) {
            for (int j = 0; j < board[0].length; j++) {
                if (board[i][j] > 1) board[i][j] -= 2;
            }
        }
    }
}
